<template>
<div>
<el-card shadow="never" style="min-height:650px;">
<div v-if="search_visible" id="search" class="search">
	<el-form ref="form" size="small" :model="searchData" inline>
		<el-form-item label="可兑次数">
			<el-input id="num" v-model="searchData.num"  style="width:150px;" placeholder="请输入可兑次数"></el-input>
		</el-form-item>
		<el-form-item label="兑换人">
			<select-page :is_clear="is_clear" url="/admin/Redemption/getMember_id" :selectval.sync="searchData.member_id"></select-page>
		</el-form-item>
		<el-form-item label="状态">
			<el-select style="width:150px" v-model="searchData.status" filterable clearable placeholder="请选择">
				<el-option key="0" label="待导出" value="1"></el-option>
				<el-option key="1" label="待兑换" value="2"></el-option>
				<el-option key="2" label="已兑换" value="3"></el-option>
			</el-select>
		</el-form-item>
		<Search :search_data.sync="searchData" @refesh_list="index"></Search>
	</el-form>
</div>
<div class="btn-group" style="margin-top:10px;margin-bottom:10px;">
	<div>
		<el-button v-for="item in button_group" v-if="checkPermission(item.access)" :key="item.access"  :disabled="$data[item.disabled]" :type="item.color" size="mini" :icon="item.icon" @click="fn(item.clickname)">
			<span v-if="item.batch" v-text="$data['batchUpdateStatus']?'批量保存':'批量编辑'"></span>
			<span v-else v-text="item.name"></span>
		</el-button>
	</div>
	<div><TableTool :search_visible.sync="search_visible"   @refesh_list="index"></TableTool></div>
</div>
<el-table :row-class-name="rowClass" @selection-change="selection"  @row-click="handleRowClick"  row-key="redemption_id"  :header-cell-style="{ background: '#eef1f6', color: '#606266' }" @sort-change='sortChange' v-loading="loading"  ref="multipleTable" border class="eltable" :data="list"  style="width: 100%">
	<el-table-column align="center" type="selection" width="42"></el-table-column>
	<el-table-column align="center" type = '' property="redemption_id"  label="编号"  width="70">
	</el-table-column>
	<el-table-column align="center"  property="code"  label="兑换码" show-overflow-tooltip width="">
	</el-table-column>
	<el-table-column align="center"  property="num"  label="可兑次数" show-overflow-tooltip width="">
	</el-table-column>
	<el-table-column align="center"  property="member.nick_name"  label="兑换人" show-overflow-tooltip width="">
	</el-table-column>
	<el-table-column align="center"  property="recieve_time"  label="兑换时间" show-overflow-tooltip width="">
		<template slot-scope="scope">
			{{parseTime(scope.row.recieve_time)}}
		</template>
	</el-table-column>
	<el-table-column align="center"  property="status"  label="状态" show-overflow-tooltip width="">
		<template slot-scope="scope">
			<el-tag type="primary" v-if="scope.row.status == '1'" size="mini" effect="dark">待导出</el-tag>
			<el-tag type="warning" v-if="scope.row.status == '2'" size="mini" effect="dark">待兑换</el-tag>
			<el-tag type="success" v-if="scope.row.status == '3'" size="mini" effect="dark">已兑换</el-tag>
		</template>
	</el-table-column>
</el-table>
<Page :total="page_data.total" :page.sync="page_data.page" :limit.sync="page_data.limit" @pagination="index" />
</el-card>

<!--添加-->
<Add :show.sync="dialog.addDialogStatus" size="small" @refesh_list="index"></Add>
<!--批量添加-->
<Adds :show.sync="dialog.addsDialogStatus" size="small" @refesh_list="index"></Adds>
<!--导出弹窗-->
<el-dialog title="导出进度条" :visible="dumpshow" :before-close="closedialog" width="500px">
	<el-progress :percentage="percentage"></el-progress>
</el-dialog>

</div>
</template>

<script>
import Search from '@/components/common/Search.vue'
import Page from '@/components/common/Page.vue'
import TableTool from '@/components/common/TableTool.vue'
import SelectPage from '@/components/common/SelectPage.vue'
import XLSX from "xlsx"
import Add from '@/views/admin/redemption/add.vue'
import Adds from '@/views/admin/redemption/adds.vue'
export default {
	components: {
		Search,
		Page,
		TableTool,
		SelectPage,
		Add,
		Adds,
	},
	data: function() {
		return {
			dialog: {
				addDialogStatus : false,
				addsDialogStatus : false,
			},
			searchData:{},
			button_group:[
				{name:'添加',color:'success',access:'/admin/Redemption/add',icon:'el-icon-plus',disabled:'',clickname:'add'},
				{name:'批量添加',color:'warning',access:'/admin/Redemption/adds',icon:'el-icon-plus',disabled:'',clickname:'adds'},
				{name:'导出',color:'warning',access:'/admin/Redemption/dumpdata',icon:'el-icon-download',disabled:'',clickname:'dumpdata'},
				{name:'设为已导出',color:'primary',access:'/admin/Redemption/changeStatus',icon:'el-icon-edit-outline',disabled:'multiple',clickname:'changeStatus'},
			],
			loading: false,
			page_data: {
				limit: 20,
				page: 1,
				total:20,
			},
			order:'',
			sort:'',
			ids: [],
			single:true,
			multiple:true,
			search_visible:true,
			list: [],
			exceldata:[],
			dumppage:1,
			ws:{},
			dumpshow:false,
			percentage:0,
			filename:'',
			is_clear:false,
		}
	},
	methods:{
		index(){
			let param = {limit:this.page_data.limit,page:this.page_data.page,order:this.order,sort:this.sort}
			Object.assign(param, this.searchData,this.$route.query)
			this.loading = true
			this.$axios.post('/admin/Redemption/index',param).then(res => {
				if(res.data.status == 200){
					this.list = res.data.data.data
					this.page_data.total = res.data.data.total
					this.loading = false
				}else{
					this.$message.error(res.data.msg);
				}
			})
		},
		add(){
			this.dialog.addDialogStatus = true
		},
		adds(){
			this.dialog.addsDialogStatus = true
		},
		dumpdata(){
			this.$confirm('确定操作吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.dumpshow = true
				this.confirmdumpdata()
			}).catch(() => {})
		},
		confirmdumpdata(){
			let query = {page:this.dumppage,order:this.order,sort:this.sort}
			Object.assign(query, this.searchData,{redemption_id:this.ids.join(',')},this.urlobj)
			this.$axios.post('/admin/Redemption/dumpdata',query).then(res => {
				if(res.data.data && res.data.data.length > 0){
					if(this.dumppage == 1){
						this.exceldata.push(res.data.header)
					}
					res.data.data.forEach((item) => {
						this.exceldata.push(Object.values(item))
					})
					this.percentage = res.data.percentage
					this.filename = res.data.filename
					this.ws = XLSX.utils.aoa_to_sheet(this.exceldata)
					this.dumppage = this.dumppage + 1
					this.confirmdumpdata()
				}else{
					let wb = XLSX.utils.book_new()
					XLSX.utils.book_append_sheet(wb, this.ws)
					XLSX.writeFile(wb, this.filename)
					this.exceldata = []
					this.dumpshow = false
					this.dumppage = 1
					this.percentage = 0
				}
			})
		},
		closedialog(){
			this.dumpshow = false
		},
		changeStatus(row){
			this.$confirm('确定操作吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let ids = row.redemption_id ? row.redemption_id : this.ids.join(',')
				this.$axios.post('/admin/Redemption/changeStatus',{redemption_id:ids}).then(res => {
					if(res.data.status == 200){
						this.$message({message: res.data.msg, type: 'success'})
						this.index()
					}else{
						this.$message.error(res.data.msg)
					}
				})
			}).catch(() => {})
		},
		selection(selection) {
			this.ids = selection.map(item => item.redemption_id)
			this.single = selection.length != 1
			this.multiple = !selection.length
		},
		handleRowClick(row, rowIndex,event){
			if(event.target.className !== 'el-input__inner'){
				this.$refs.multipleTable.toggleRowSelection(row)
			}
		},
		rowClass ({ row, rowIndex }) {
			for(let i=0;i<this.ids.length;i++) {
				if (row.redemption_id === this.ids[i]) {
					return 'rowLight'
				}
			}
		},
		sortChange(val){
			if(val.order == 'descending'){
				this.order= 'desc'
			}
			if(val.order == 'ascending'){
				this.order= 'asc'
			}
			this.sort = val.prop
			this.index()
		},
		fn(method){
			this[method](this.ids)
		},
	},
	mounted(){
		this.index()
	},
}
</script>

<style  lang="scss">
@import '@/assets/scss/common.scss'
</style>
